package study.kit.el.zh.table;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.map.multi.RowKeyTable;
import cn.hutool.core.map.multi.Table;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import study.kit.el.zh.parse.RangeVO;
import study.kit.el.zh.parse.ZhParser;

import java.nio.charset.Charset;
import java.util.List;

/**
 * 解析数据
 */
@Slf4j
public class RangeUtil {

    /**
     * 合并坐标
     *
     * @param rangeVOLt
     * @return Table<Integer, Integer, Tuple>
     */
    public static Table<Integer, Integer, Tuple> getMergeCoord(List<RangeVO> rangeVOLt) {
        //
        Table<Integer, Integer, Tuple> table = new RowKeyTable<>();
        //
        for (RangeVO range : rangeVOLt) {
            log.info(">>>>>> {}", range);
            int rowNum = range.getRowNum(), colNum = range.getColNum();
            int rowCnt = range.getRowCnt(), colCnt = range.getColCnt();
            //
            if (rowCnt > 1 && colCnt == 1) {
                table.putAll(rowTraversal(rowNum, colNum, rowCnt));
            }
            if (colCnt > 1 && rowCnt == 1) {
                table.putAll(colTraversal(rowNum, colNum, colCnt));
            }
            if (rowCnt > 1 && colCnt > 1) {
                table.putAll(mixedTraversal(rowNum, colNum, rowCnt, colCnt));
            }
        }
        log.info("{}", table);
        //
        return table;
    }

    /**
     * 行遍历
     *
     * @param rowNum
     * @param colNum
     * @param rowCnt
     * @return Table<Integer, Integer, Tuple>
     */
    private static Table<Integer, Integer, Tuple> rowTraversal(int rowNum, int colNum, int rowCnt) {
        //
        Table<Integer, Integer, Tuple> table = new RowKeyTable<>();
        Tuple parent = new Tuple(rowNum, colNum);
        //
        for (int i = 0; i < rowCnt; i++) {
            //
            log.info("({}, {})", rowNum, colNum);
            table.put(rowNum, colNum, parent);

            // 使用完之后
            rowNum = rowNum + 1;
        }
        return table;
    }

    /**
     * 列变量
     *
     * @param rowNum
     * @param colNum
     * @param colCnt
     * @return Table<Integer, Integer, Tuple>
     */
    private static Table<Integer, Integer, Tuple> colTraversal(int rowNum, int colNum, int colCnt) {
        //
        Table<Integer, Integer, Tuple> table = new RowKeyTable<>();
        Tuple parent = new Tuple(rowNum, colNum);
        //
        for (int i = 0; i < colCnt; i++) {
            //
            log.info("({}, {})", rowNum, colNum);
            table.put(rowNum, colNum, parent);
            // 使用完之后
            colNum = colNum + 1;
        }
        return table;
    }

    /**
     * 行列遍历
     *
     * @param rowNum
     * @param colNum
     * @param rowCnt
     * @param colCnt
     * @return Table<Integer, Integer, Tuple>
     */
    private static Table<Integer, Integer, Tuple> mixedTraversal(int rowNum, int colNum, int rowCnt, int colCnt) {

        log.info("{}, {}, {}, {}", rowNum, colNum, rowCnt, colCnt);
        //
        Table<Integer, Integer, Tuple> table = new RowKeyTable<>();
        Tuple parent = new Tuple(rowNum, colNum);
        // 行
        for (int i = 0; i < rowCnt; i++) {
            // 列
            int myColNum = colNum;
            for (int j = 0; j < colCnt; j++) {
                //
//              log.info("第 {} 行, 第 {} 列", rowNum, myColNum);
                log.info("({}, {})", rowNum, myColNum);
                table.put(rowNum, myColNum, parent);

                // 使用完之后
                myColNum = myColNum + 1;

                // 重置列
                if (myColNum - colCnt == colNum) {
                    myColNum = colNum;
                }
            }

            // 使用完之后
            rowNum = rowNum + 1;
        }

        return table;
    }
}
